const util = require('util');
const fs = require('fs');
const path = require('path');

const unlink = util.promisify(fs.unlink);
const readdir = util.promisify(fs.readdir);

module.exports = app => {
	return {
		schedule: {
			cron: '0 0 4 * * *',
			type: 'worker',
			immediate: true,
		},
		async task(ctx) {
			console.log('=============', 'delete token', '=================');
			const tokenPath = path.resolve('wechat_logs');
			if (fs.existsSync(tokenPath)) {
				const files = await readdir(tokenPath);
				files.forEach((file, index) => {
					if (file.indexOf(':access_token.txt') > -1) {
						unlink(`${tokenPath}/${file}`);
					}
				});
			}
		},
	};
};
